<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
$return['gettype']=array('1'=>'充值兑换','2'=>'活动赠送','3'=>'朋友赠送');
if($_RQ['op']=='get'){
	$return['list']=pdo_getall('jy_weishop_goodscard',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'gettype'=>1),array('id','ishot','price','money'));
	$return['listgame']=pdo_getall('jy_weishop_goodscard',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'gettype'=>2),array('id','ishot','price','money'));
	exi($return);
}
if($_RQ['op']=='buy'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('请选择购物卡！','error');
	}
	$card=pdo_get('jy_weishop_goodscard',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$getallnum = pdo_count('jy_weishop_goodscard_getlog',array('uniacid'=>$_SESSION['uniacid'],'cardid'=>$id));
	if($card['t'] <= $getallnum && $card['t']>0){
			exi('不能再领了，已经上限了!','error');
	}
	$getnum = pdo_count('jy_weishop_goodscard_getlog',array('uniacid'=>$_SESSION['uniacid'],'cardid'=>$id,'mid'=>$_SESSION['uid']));
	if($getnum && $card['getmax'] && $card['getmax'] <= $getnum){
			exi( '不能再领了，已经上限了!','error');
	}
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'cardid'=>intval($post['id']),
		'ordersn'=>get_ordersn('jy_weishop_goodscard_order','ordersn'),
		'price'=>$card['price'],
		'ispay'=>0,
		'status'=>0,
		'num'=>1,
		'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_weishop_goodscard_order',$data);
	$id=pdo_insertid();
	$params=array(
		'action'=>'memebr.getcard',
		'id'=>$id,
		'fee'=>$card['price'],
		'title'=>'兑换购物卡',
		'plugin'=>'jy_weishop',
		'pay_type'=>'wechat',
		'back_url'=>'jy_weishop/member.getcard/pay_result/',
		'success_url'=>'jy_weishop/member.mycard/',
		'error_url'=>'jy_weishop/member.mycard/',
	);
	$pay_id=cfc('pay')->create_pay_order($params);
	exi($pay_id);
}
if($_RQ['op']=='getcard'){
	$post=get_postdata();
	if(!intval($post['id'])){
		exi('请选择购物卡！','error');
	}
	$this->model('goodscard')->getcard(intval($post['id']),2);
	exi('领取成功！','','jy_weishop/member.mycard/');
}
if($_RQ['op']=='pay_result'){
	$order=pdo_get('jy_weishop_goodscard_order',array('id'=>$_RQ['pay_result']['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),array('id','price','status','cardid'));
	if($order['price']!=$_RQ['pay_result']['fee']){
		exi('支付失败，支付金额不匹配！','error');
	}
	if($order['status']){
		exi('该订单已支付！无需重复支付！','error');
	}
	if($_RQ['pay_result']['status']!=1){
		exi('兑换失败！','error');
	}
	$this->model('goodscard')->getcard(intval($order['cardid']),1);
	pdo_update('jy_weishop_goodscard_order',array('status'=>1,'ispay'=>1,'paytype'=>$_RQ['pay_result']['pay_type']),array('id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
	exi('兑换成功！');
}